
--
-- stack to store the commands so we can undo/redo
-- has a fixed maximum size, and will go back to the beginning
-- and over write values once full.
--


-- set max to a much larger number, once undo/redo is more stable.
commandstack = {max=50, top=1, depth=0}

-- put a command on the stack
-- @param s - the stack to push the value on to.
-- @param value - the value to push on to the stack.
-- @returns nothing
function commandstack.push (s, value)
   s.depth=0
   s[s.top] = value
   s.top = (s.top+1)%s.max
end

-- different undo message depending on failure, useful for
-- debugging.
function commandstack.undo(s)
   if s.depth < s.max-1 then
      s.depth = s.depth+1
      local f = s[(s.top-s.depth)%s.max]
      if f then 
	 f.undo() 
	 return "undo!"
      else 
	 return " no more to undo!"
      end
   else
      return " no more to UNDO!"
   end   
end

function commandstack.redo(s)   
   if s.depth > 0 then
      local f = s[(s.top-s.depth)%s.max]
      if f then 
	 f.redo() 
	 s.depth = s.depth-1
	 return "redo!"
      else 
	 return "no more to redo" 
      end
   else
      return " no more to REDO!"
   end
end

-- macro stack class.

macrostack = {}
macrostack.__index = macrostack

function macrostack.new()
	return setmetatable({stack={}}, macrostack)
end

--append
function macrostack:append(val)
	self.stack[#self.stack +1]=val
end

function macrostack:ipairs()
	return ipairs(self.stack)
end